//
//  EditShopInfoViewController.m
//  XingHeHuiJu
//
//  Created by 于顺燊 on 2018/11/9.
//  Copyright © 2018年 cywkj. All rights reserved.
//

#import "EditShopInfoViewController.h"
#import "UIButton+WebCache.h"
#import "TZImagePickerController.h"
#import "BRPickerView.h"
#import "ShopLocationInfo.h"

@interface EditShopInfoViewController ()<UITextFieldDelegate, UITextViewDelegate>

@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) ShopLocationInfo *shopInfo;
@property (strong, nonatomic) NSString *imgStr;

@end

@implementation EditShopInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[self createUI];
	[self getShopInfomation];
}

- (void)createUI {
	self.navigationItem.title = @"编辑商家信息";
	_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
	_scrollView.backgroundColor = UISeparatedColorRGB;
	[self.view addSubview:_scrollView];
	NSArray *arr = [NSArray arrayWithObjects:@"   店铺头像*", @"   店铺名称*", @"   商家电话*", @"   营业时间*", @"   宣传签名*", nil];
	
	CGSize size = [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(15)];
	
	for (int i = 0; i < arr.count; i++) {
		UILabel *label = [[UILabel alloc] init];
		[_scrollView addSubview:label];
		label = [self createLabel:label text:arr[i]];
		label.tag = 1+i;
	}
	UILabel *headPic = [self.view viewWithTag:1];
	headPic.frame = CGRectMake(0, 0, SCREEN_WIDTH, size.height+30);
	
	UIButton *headPicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	[_scrollView addSubview:headPicBtn];
	headPicBtn.frame = CGRectMake(SCREEN_WIDTH-SW(20)-20-size.height, 5,  20+size.height,  20+size.height);
	headPicBtn.layer.cornerRadius = (20+size.height)/2;
	headPicBtn.layer.masksToBounds = YES;
	[headPicBtn addTarget:self action:@selector(headPicAction:) forControlEvents:UIControlEventTouchUpInside];
	headPicBtn.tag = 21;
	
	UILabel *shopName = [self.view viewWithTag:2];
	shopName.frame = CGRectMake(0, headPic.y+headPic.height+1, SCREEN_WIDTH, size.height+30);
	UITextField *shopNameTF = [self createTextFieldWith:shopName tag:22 pickerNum:0];
	
	UILabel *phone = [self.view viewWithTag:3];
	phone.frame = CGRectMake(0, shopName.y+shopName.height+1, SCREEN_WIDTH, size.height+30);
	UITextField *phoneTF = [self createTextFieldWith:phone tag:23 pickerNum:0];
	
	
	UILabel *bussinessTime = [self.view viewWithTag:4];
	bussinessTime.frame = CGRectMake(0, phone.y+phone.height+1, SCREEN_WIDTH, size.height+30);
	UITextField *bussinessTimeTF = [self createTextFieldWith:bussinessTime tag:24 pickerNum:0];
	
	UILabel *sign = [self.view viewWithTag:5];
	sign.frame = CGRectMake(0, bussinessTime.y+bussinessTime.height+1, SCREEN_WIDTH, size.height+30);
	UITextView *signTV = [self createTextViewWith:sign tag:25 heightFactor:1.5];
	
	//保存按钮
	UIButton *saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, signTV.y+signTV.height+20,  SCREEN_WIDTH-30*2, 40)];
	saveBtn.layer.cornerRadius = saveBtn.height/4;
	[saveBtn setTitle:@"保存" forState:UIControlStateNormal];
	[saveBtn setBackgroundColor:UIMainColorRGB];
	[saveBtn addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
	[_scrollView addSubview:saveBtn];
	
	_scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, saveBtn.y+saveBtn.height+20);
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
	if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
		return YES;
	}
	
	return YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
	if (textView.text.length > 200) {
		[self presentViewController:[LZCTools alertMessage:@"不可以超过200字"] animated:YES completion:nil];
		textView.text = [textView.text substringToIndex:200];
		[textView resignFirstResponder];
		return;
	}
}

- (UILabel *)createLabel:(UILabel *)label text:(NSString *)text {
	
	label.textColor = [UIColor grayColor];
	label.text = text;
	label.font = kUHSystemFontWithSize(15);
	label.backgroundColor = kWhiteColor;
	return label;
}

- (UITextView *)createTextViewWith:(UILabel *)label tag:(NSInteger)tag heightFactor:(CGFloat)heightFactor
{
	UITextView *textView = [[UITextView alloc] init];
	[_scrollView addSubview:textView];
	textView.textColor = [UIColor lightGrayColor];
	textView.font = kUHSystemFontWithSize(14);
	textView.delegate = self;
	[textView scrollRangeToVisible:NSMakeRange(0,1)];
	CGSize signSize = [textView sizeThatFits:CGSizeMake(SCREEN_WIDTH-20-SW(20), MAXFLOAT)];
	textView.frame = CGRectMake(0, label.y+label.height+1, SCREEN_WIDTH, 15+signSize.height*heightFactor);
	textView.textContainerInset = UIEdgeInsetsMake(15, SW(20), 15, 20);//设置页边
	textView.tag = tag;
	return textView;
}

- (UITextField *)createTextFieldWith:(UILabel *)label tag:(NSInteger)tag pickerNum:(NSInteger)picker
{
	CGSize size = [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(15)];
	UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30+size.width, label.y, SCREEN_WIDTH-(50+size.width), size.height+30)];
	[_scrollView addSubview:textField];
	textField.delegate = self;
	textField.textColor = kGrayColor;
	textField.clearButtonMode = UITextFieldViewModeWhileEditing;
	textField.font = kUHSystemFontWithSize(15);
	textField.textAlignment = NSTextAlignmentRight;
	textField.rightViewMode = UITextFieldViewModeAlways;
	textField.tag = tag;
	UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.height/2, size.height)];
	textField.rightView = imgV;
	imgV.image = IMAGE(@"arrow_right_grey");
	//可以选择把选择器动作加到这里
	if (picker != 0) {
		
	}
	else {
		
	}
	return textField;
}

- (void)headPicAction:(UIButton *)sender {
	TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 columnNumber:4 delegate:nil pushPhotoPickerVc:YES];
	
	//不展示相册中的视频
	imagePickerVc.allowPickingVideo = NO;
	//展示图片
	imagePickerVc.allowPickingImage = YES;
	//不显示原图选项
	imagePickerVc.allowPickingOriginalPhoto = NO;
	//按时间排序
	imagePickerVc.sortAscendingByModificationDate = YES;
	
	[imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
		[[NetworkTool sharedTool] uploadImage:photos[0] callBack:^(id responseObject) {
			NSArray *imgArr = responseObject[@"data"][@"images"];
			self.imgStr = imgArr[0];
			UIButton *shopImgBtn = (UIButton *)[self.view viewWithTag:21];
			[shopImgBtn setBackgroundImage:photos[0] forState:UIControlStateNormal];
		}];
	}];
	[self presentViewController:imagePickerVc animated:YES completion:nil];
}

- (void)assignValue {
	UIButton *shopImgBtn = (UIButton *)[self.view viewWithTag:21];
	[shopImgBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:self.shopInfo.photo] forState:UIControlStateNormal placeholderImage:IMAGE(@"placeHolder")];
	self.imgStr = self.shopInfo.photo;
	
	UITextField *shopName = (UITextField *)[self.view viewWithTag:22];
	UITextField *phone = (UITextField *)[self.view viewWithTag:23];
	UITextField *bussinessTime = (UITextField *)[self.view viewWithTag:24];
	UITextView *sign = (UITextView *)[self.view viewWithTag:25];
	shopName.text = self.shopInfo.shop_name;
	phone.text = self.shopInfo.tel;
	bussinessTime.text = self.shopInfo.shop_hours;
	sign.text = self.shopInfo.sign;
}

- (void)saveAction {
	
	UITextField *shopName = (UITextField *)[self.view viewWithTag:22];
	UITextField *phone = (UITextField *)[self.view viewWithTag:23];
	UITextField *bussinessTime = (UITextField *)[self.view viewWithTag:24];
	UITextView *sign = (UITextView *)[self.view viewWithTag:25];
	
	NSDictionary *param = @{
							@"shop_id":self.shopInfo.shop_id,
							@"photo":self.imgStr,
							@"shop_name":shopName.text,
							@"tel":phone.text,
							@"shop_hours":bussinessTime.text,
							@"sign":sign.text,
							};
	[[NetworkTool sharedTool] requestWithName:@"更新商家信息" URLString:APIUpdateShopInfo parameters:param method:@"POST" callBack:^(id responseObject) {
		[self presentViewController:[LZCTools alertMessage:@"保存成功"] animated:YES completion:nil];
		[self.navigationController popViewControllerAnimated:YES];
	}];
}

- (void)getShopInfomation {
	[[NetworkTool sharedTool] requestWithName:@"获取商家信息" URLString:APIGetShopInfo parameters:nil method:@"GET" callBack:^(id responseObject) {
		NSDictionary *dict = responseObject[@"data"];
		[self.shopInfo setValuesForKeysWithDictionary:dict];
		[self assignValue];
	}];
}

- (ShopLocationInfo *)shopInfo {
	if (!_shopInfo) {
		_shopInfo = [[ShopLocationInfo alloc] init];
	}
	return _shopInfo;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
